<template>
    <div class="excelModule">
        <el-button type="primary" size="small" 
            @click="downExcelModule" >Excel 模板下载</el-button>
    </div>
</template>
/* Excel 模板下载 */
<script>
import FileSaver from 'file-saver';
import ExcelJS from 'exceljs';
export default {
    name: 'ExcelModule',
    data() {
        return {

        }
    },
    created() {
        // https://www.mk2048.com/blog/blog_bjkikjibi2ab.html
        // https://www.codeleading.com/article/18414953950/
    },
    methods: {
        async downExcelModule() {
            console.log('下载Excel模板！！！');
            const Workbook = new ExcelJS.Workbook();
            const Sheet1 = Workbook.addWorksheet('Sheet1');

            // 添加多级表头
            Sheet1.getRow(1).values = ['种类', '销量', '', '', '', '店铺']
            Sheet1.getRow(2).values = ['种类', '18-050', '18-060', '18-070', '18-080', '店铺']
            // 添加数据项定义，与之前不同的是，此时去除header字段
            Sheet1.columns = [
                {key: 'category', width: 15},
                {key: '2018-05', width: 15},
                {key: '2018-06', width: 15},
                {key: '2018-07', width: 15},
                {key: '2018-08', width: 15},
                {key: 'store', width: 15},
            ]
            const data = [{
                category: '衣服',
                '2018-05': 300,
                '2018-06': 230,
                '2018-07': 730,
                '2018-08': 630,
                'store': '王小二旗舰店'
            }, {
                category: '零食',
                '2018-05': 672,
                '2018-06': 826,
                '2018-07': 302,
                '2018-08': 389,
                'store': '吃吃货'
            }]
            Sheet1.addRows(data)
            // 合并单元格
            Sheet1.mergeCells(`B1:E1`)
            Sheet1.mergeCells('A1:A2')
            Sheet1.mergeCells('F1:F2')
            // 设置每一列样式
            const row = Sheet1.getRow(1)
            row.eachCell((cell, rowNumber) => {
                // console.log(cell, rowNumber);
                Sheet1.getColumn(rowNumber).alignment = {vertical: 'middle', horizontal: 'center'}
                Sheet1.getColumn(rowNumber).font = {size: 14, family: 2}
            })


            // Sheet1.mergeCells('A1:C1');
            // Sheet1.getCell('A1').value = '人力模板！！';

            // const title = [
            //     { name: '楼栋', key: 'building', },
            //     { name: '日期', key: 'date', },
            //     { name: '人力', key: 'peopleNumber', },
            // ]
            // const Thead = []
            // for (const item of title) {
            //     Thead.push({
            //         header: item['name'],
            //         key: item['key'],
            //         width: item['width'] || 15,
            //         style: { // 样式
            //             alignment: {
            //                 vertical: 'middle', 
            //                 horizontal: 'center'
            //             },
            //         },
            //     })
            // }
            // Sheet1.columns = Thead
            // Sheet1.addRow([3, 'Sam', new Date()]);
            // Sheet1.addRow({building: '1#', date: new Date(1970,1,1), peopleNumber: '111', });

            // Sheet1.columns = []
            // // 动态数据，添加行数据
            // Sheet1.addRows([{a:1}]);


            // Sheet1.addTable({
            //     name: 'MyTable',
            //     ref: 'A10',
            //     headerRow: true,
            //     totalsRow: true,
            //     style: {
            //         theme: 'TableStyleDark3',
            //         showRowStripes: true,
            //     },
            //     columns: [
            //         {name: 'Date', totalsRowLabel: 'Totals:', filterButton: true},
            //         {name: 'Amount', totalsRowFunction: 'sum', filterButton: false},
            //     ],
            //     rows: [
            //         [new Date('2019-07-20'), 70.10],
            //         [new Date('2019-07-21'), 70.60],
            //         [new Date('2019-07-22'), 70.10],
            //     ],
            // });
            

            const buffer = await Workbook.xlsx.writeBuffer();
            const blob = new Blob([buffer], {
                type: "application/octet-stream; charset=utf-8"
            });

            // 使用FileSave 保存文件
            FileSaver.saveAs(blob, "模板下载.xlsx");
        }

    }
}
</script>
